package com.xinghou.platform.dao.main.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

import com.xinghou.platform.dao.main.common.DBStoreLevelCached;
import com.xinghou.platform.model.main.StoreBrowse;
import com.xinghou.platform.util.JsonUtil;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class StoreBroweCacheAspect extends DBStoreLevelCached {
	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreBrowseDAO.getStoreBrowseByid(..))")
	public void getStoreBrowseByid(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreBrowseDAO.getStoreBrowseBytwo(..))")
	public void getStoreBrowseBytwo(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.StoreBrowseDAO.updateStoreBrowse(..))")
	public void updateStoreBrowse(){
	}

	@Around("getStoreBrowseByid()")
	public StoreBrowse getStoreBrowseByid(ProceedingJoinPoint pjp) throws Throwable{
		String id = ((Object[])pjp.getArgs())[0].toString();
		String key = String.format(CACHE_STOREBROW_UID , id);
		Object jsonToken = getRedisClient().get(key, CACHED_STORE_DB_INDEX);
		if(jsonToken == null){
			StoreBrowse storeBrowse = (StoreBrowse)pjp.proceed();
			if(storeBrowse != null)
				getRedisClient().set(key, JsonUtil.toJson(storeBrowse), CACHED_STORE_DB_INDEX);
			return storeBrowse;
		}else{
			StoreBrowse storeBrowse = (StoreBrowse)JsonUtil.fromJson(jsonToken.toString(), StoreBrowse.class);
			return storeBrowse;
		}
	}

	@Around("getStoreBrowseBytwo()")
	public StoreBrowse getStoreBrowseBytwo(ProceedingJoinPoint pjp) throws Throwable{
		String userId = ((Object[])pjp.getArgs())[0].toString();
		String cardId = ((Object[])pjp.getArgs())[1].toString();
		String key = String.format(CACHE_STOREBROW_USID , userId,cardId);
		Object jsonToken = getRedisClient().get(key, CACHED_STORE_DB_INDEX);
		if(jsonToken == null){
			StoreBrowse storeBrowse = (StoreBrowse)pjp.proceed();
			if(storeBrowse != null)
				getRedisClient().set(key, JsonUtil.toJson(storeBrowse), CACHED_STORE_DB_INDEX);
			return storeBrowse;
		}else{
			StoreBrowse storeBrowse = (StoreBrowse)JsonUtil.fromJson(jsonToken.toString(), StoreBrowse.class);
			return storeBrowse;
		}
	}

	@Around("updateStoreBrowse()")
	public int updateStoreBrowse(ProceedingJoinPoint pjp) throws Throwable{
		
		StoreBrowse storeBrowse = (StoreBrowse)((Object[])pjp.getArgs())[0];
		
		int returnno = (int) pjp.proceed();
		
		String keyInfoId = String.format(CACHE_STOREBROW_UID , storeBrowse.getId().toString());
		
		if(getRedisClient().exists(keyInfoId, CACHED_STORE_DB_INDEX)){
			getRedisClient().set(keyInfoId, JsonUtil.toJson(storeBrowse), CACHED_STORE_DB_INDEX);
		}
		
		keyInfoId = String.format(CACHE_STOREBROW_USID , storeBrowse.getUserId().toString(),storeBrowse.getStoreId().toString());
		
		if(getRedisClient().exists(keyInfoId, CACHED_STORE_DB_INDEX)){
			getRedisClient().set(keyInfoId, JsonUtil.toJson(storeBrowse), CACHED_STORE_DB_INDEX);
		}
		
		return returnno;
	}
	
}
